package cn.itcast.web;

import cn.itcast.annotation.PreAuthorize;
import cn.itcast.result.PageBean;
import cn.itcast.result.Result;
import cn.itcast.service.EmployeeService;
import cn.itheima.sys.dto.EmployeeDTO;
import cn.itheima.sys.dto.EmployeeLoginDTO;
import cn.itheima.sys.dto.EmployeePageQueryDTO;
import cn.itheima.sys.dto.PasswordEditDTO;
import cn.itheima.sys.vo.EmployeeLoginVO;
import cn.itheima.sys.vo.EmployeeVO;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/employee")
@Api(tags = "员工相关接口")
@Slf4j
@AllArgsConstructor
public class EmployeeController {

    private EmployeeService employeeService;

    /**
     * 获取员工分页列表
     *
     * @return 员工分页列表
     */
    @PreAuthorize("admin:employee:list")
    @GetMapping("/page")
    public Result<PageBean> pageList(EmployeePageQueryDTO condition) {
        return Result.success(employeeService.pageList(condition));
    }

    /**
     * 获取所有员工
     *
     * @return 员工列表
     */
    @PreAuthorize("admin:employee:list")
    @GetMapping("/list")
    public Result<List<EmployeeVO>> list() {
        return Result.success(employeeService.list());
    }

    /**
     * 添加员工信息
     *
     * @param emp 员工
     * @return Result
     */
    @PreAuthorize("admin:employee:add")
    @PostMapping
    public Result<String> add(@RequestBody EmployeeDTO emp) {
        employeeService.add(emp);
        return Result.success();
    }

    /**
     * 修改员工信息
     *
     * @param emp 员工
     * @return Result
     */
    @PreAuthorize("admin:employee:update")
    @PutMapping
    public Result<String> update(@RequestBody EmployeeDTO emp) {
        employeeService.update(emp);
        return Result.success();
    }

    /**
     * 根据Id查询员工信息
     *
     * @return EmployeeVO
     */
    @PreAuthorize("admin:employee:list")
    @GetMapping("/{id}")
    public Result<EmployeeVO> getById(@PathVariable Long id) {
        EmployeeVO employeeVO = employeeService.getById(id);
        return Result.success(employeeVO);
    }

    /**
     * 员工登录
     *
     * @param employeeLoginDTO 登录信息
     * @return jwt令牌
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        EmployeeLoginVO employeeLoginVO = employeeService.login(employeeLoginDTO);
        return Result.success(employeeLoginVO);
    }

    /**
     * 修改员工状态
     *
     * @return 无
     */
    @PreAuthorize("admin:employee:update")
    @PostMapping("/status/{status}")
    public Result<String> updateStatus(@PathVariable Integer status, Long id) {
        employeeService.updateStatus(status, id);
        return Result.success();
    }

    /**
     * 修改员工密码
     *
     * @return 无
     */
    @PreAuthorize("admin:employee:update")
    @PutMapping("/editPassword")
    public Result<String> editPassword(@RequestBody PasswordEditDTO passwordEditDTO) {
        employeeService.editPassword(passwordEditDTO);
        return Result.success();
    }
}
